const fs = require('fs');
const path = require('path');
const { ipcRenderer } = require('electron');

// 写入日志内容
const writeLog = (msg) => {
    ipcRenderer.send('placeAnOrderWindow_click_msg', { type: '修改写入条件单' })
    let logDirectory = path.resolve(__dirname, '../../../../../../static/tiaojian');
    if (process.env.NODE_ENV == 'production') {
        logDirectory = path.resolve(__dirname, '../../static/tiaojian');
    }
    const logFile = path.join(logDirectory, 'tiaojian.json');
    if (!fs.existsSync(logDirectory)) {
        fs.mkdirSync(logDirectory);
    }
    try {
        fs.writeFileSync(logFile, msg, 'utf-8');
        console.log(`output->ms`, logFile)
    } catch (err) {
        console.error('创建或写入日志文件时出错:', err);
    }
};

// 读取过去指定天数的日志内容
const readLogs = () => {
    let logDirectory = path.resolve(__dirname, '../../../../../../static/tiaojian');
    if (process.env.NODE_ENV == 'production') {
        logDirectory = path.resolve(__dirname, '../../static/tiaojian');
    }
    let allLogs = ''; // 存储所有日志内容
    const logFile = path.join(logDirectory, 'tiaojian.json');
    if (fs.existsSync(logFile)) {
        try {
            const content = fs.readFileSync(logFile, 'utf-8');
            allLogs = content;
        } catch (err) {
            allLogs = ''
        }
    } else {
        allLogs = ''
    }
    return allLogs; // 返回读取到的所有日志内容
};

// 写入画线信息
const writeHuaXian = (msg) => {
    let logDirectory = path.resolve(__dirname, '../../../../../../static/tiaojian');
    if (process.env.NODE_ENV == 'production') {
        logDirectory = path.resolve(__dirname, '../../static/tiaojian');
    }
    const logFile = path.join(logDirectory, 'hauxian.json');
    if (!fs.existsSync(logDirectory)) {
        fs.mkdirSync(logDirectory);
    }
    try {
        fs.writeFileSync(logFile, msg, 'utf-8');
    } catch (err) {
        console.error('创建或写入日志文件时出错:', err);
    }
};

// 读取画线信息
const readHuaXian = () => {
    let logDirectory = path.resolve(__dirname, '../../../../../../static/tiaojian');
    if (process.env.NODE_ENV == 'production') {
        logDirectory = path.resolve(__dirname, '../../static/tiaojian');
    }
    let allLogs = ''; // 存储所有日志内容
    const logFile = path.join(logDirectory, 'hauxian.json');
    if (fs.existsSync(logFile)) {
        try {
            const content = fs.readFileSync(logFile, 'utf-8');
            allLogs = content;
        } catch (err) {
            allLogs = ''
        }
    } else {
        allLogs = ''
    }
    return allLogs; // 返回读取到的所有日志内容
};
// 写入预警信息
const writeYuJing = (msg) => {
    let logDirectory = path.resolve(__dirname, '../../../../../../static/tiaojian');
    if (process.env.NODE_ENV == 'production') {
        logDirectory = path.resolve(__dirname, '../../static/tiaojian');
    }
    const logFile = path.join(logDirectory, 'YuJing.txt');
    if (!fs.existsSync(logDirectory)) {
        fs.mkdirSync(logDirectory);
    }
    try {
        fs.writeFileSync(logFile, msg, 'utf-8');
    } catch (err) {
        console.error('创建或写入日志文件时出错:', err);
    }
};

// 读取预警信息
const readYuJing = () => {
    let logDirectory = path.resolve(__dirname, '../../../../../../static/tiaojian');
    if (process.env.NODE_ENV == 'production') {
        logDirectory = path.resolve(__dirname, '../../static/tiaojian');
    }
    let allLogs = ''; // 存储所有日志内容
    const logFile = path.join(logDirectory, 'YuJing.txt');
    if (fs.existsSync(logFile)) {
        try {
            const content = fs.readFileSync(logFile, 'utf-8');
            allLogs = content;
        } catch (err) {
            allLogs = ''
        }
    } else {
        allLogs = ''
    }
    return allLogs; // 返回读取到的所有日志内容
};

module.exports = {
    writeLog,
    readLogs,
    writeHuaXian,
    readHuaXian,
    writeYuJing,
    readYuJing,
};